/**
 * 
 */
package com.lemote.action.account;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lemote.action.BaseAction;
import com.lemote.core.HibernateUtils;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.Group;
import com.lemote.entity.Model;
import com.lemote.entity.Permission;
import com.lemote.entity.Role;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.account.GroupService;
import com.lemote.service.account.ModelService;
import com.lemote.service.account.PermissionService;
import com.lemote.service.account.RoleService;
import com.lemote.utils.CheckUtils;

/**
 * @author wuhui
 * 
 *         下午12:15:24
 */
@Component("roleAction")
public class RoleAction extends BaseAction<Role, RoleService> {

	private Role entity;
	private List<Long> checkedPermissionIds;// 页面中钩选的权限id列表
	
	private List<Long> checkedModelIds;// 页面中钩选的权限id列表

	private PermissionService permissionService;

	private ModelService modelService;
	@Override
	public String delete() throws AppException {

		if (ids != null && ids.length() > 0) {
			entityService.delete(CheckUtils.stringParse(ids));
		}
		return SUCCESS;
	}

	@Override
	public String input() throws AppException {
		prepareModel();
		return SUCCESS;
	}

	@Override
	public String list() throws AppException {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	@Override
	public void prepareModel() throws AppException {
		if (id != null) {
			entity = entityService.get(id);
		} else {
			entity = new Role();
		}
	}

	@Override
	public String save() throws AppException {
		setBackUrl("role/list.htm");
		if (entity != null) {
			if (entity.getId() == null) {
				entityService.add(entity);
			} else {
				entityService.update(entity);
			}
			return GLOBALSUCCESS;
		}
		return ERROR;
	}

	/**
	 * input页面显示所有授权列表.
	 */
	public List<Permission> getAllPermissionList() {

		return permissionService.getAll();
	}
	/**
	 * input页面显示所有授权列表.
	 */
	public List<Model> getAllModelList() {
		
		return modelService.getAll();
	}


	public Role getModel() {

		return entity;
	}
	
	/**
	 * 权限操作
	 * 
	 */
	public String authorize(){
		if (id!=null) {
			entity = entityService.get(id);
			checkedPermissionIds = entity.getPermissionIds();
			checkedModelIds  = entity.getModelIds();
		}
		return SUCCESS;
	}
	
	/**
	 * 权限操作
	 * 
	 */
	public String anthorizePermission(){
		setBackUrl("role/list.htm");
		if (id==null) {
			return ERROR;
		}
		entity = entityService.get(id);
		HibernateUtils.mergeByCheckedIds(entity.getModels(), checkedModelIds, Model.class);
		HibernateUtils.mergeByCheckedIds(entity.getPermissions(), checkedPermissionIds, Permission.class);
		entityService.update(entity);
		return GLOBALSUCCESS;
	}

	@Autowired
	@Override
	public void setEntityService(RoleService entityService) {

		this.entityService = entityService;
	}

	public List<Long> getCheckedPermissionIds() {
		return checkedPermissionIds;
	}


	@Autowired
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public void setCheckedPermissionIds(List<Long> checkedPermissionIds) {
		this.checkedPermissionIds = checkedPermissionIds;
	}
	
	public List<Long> getCheckedModelIds() {
		return checkedModelIds;
	}

	public void setCheckedModelIds(List<Long> checkedModelIds) {
		this.checkedModelIds = checkedModelIds;
	}

	public void setEntity(Role entity) {
		this.entity = entity;
	}

	public Role getEntity() {
		return entity;
	}
	
	@Autowired
	public void setModelService(ModelService modelService) {
		this.modelService = modelService;
	}
	
	
}
